export const TYPES = {
  GET_LAYOUT_LIST: 'table/getlayoutlist',
  GET_LAYOUT_LIST_SUCCESS: 'table/getlayoutlistsuccess',
  GET_LAYOUT_LIST_FAIL: 'table/getlayoutlistfail',
  CREATE_LAYOUT: 'table/createlayout',
  SAVE_LAYOUT: 'table/savelayout',
  SAVE_LAYOUT_SUCCESS: 'table/savelayoutsuccess',
  SAVE_LAYOUT_FAIL: 'table/savelayoutfail',
  EDIT_EXISTING_LAYOUT: 'reward/editexistinglayout',
  CANCEL_EDIT_LAYOUT: 'table/canceleditlayout',
  DELETE_LAYOUT: 'table/deletelayout',
  DELETE_LAYOUT_SUCCESS: 'table/deletelayoutsuccess',
  DELETE_LAYOUT_FAIL: 'table/deletelayoutfail',
  GET_DESK_LIST: 'table/getdesklist',
  GET_DESK_LIST_SUCCESS: 'table/getdesklistsuccess',
  GET_DESK_LIST_FAIL: 'table/getdesklistfail',
  ADD_DESK: 'table/adddesk',
  CANCEL_EDIT_DESK: 'table/canceleditdesk',
  SAVE_DESK: 'table/savedesk',
  SAVE_DESK_SUCCESS: 'table/savedesksuccess',
  SAVE_DESK_FAIL: 'table/savedeskfail',
  SAVE_SEATS: 'table/saveseats',
  SAVE_SEATS_SUCCESS: 'table/saveseatssuccess',
  SAVE_SEATS_FAIL: 'table/saveseatsfail',
  DELETE_DESK: 'table/deletedesk',
  DELETE_DESK_SUCCESS: 'table/deletedesksuccess',
  DELETE_DESK_FAIL: 'table/deletedeskfail',
  EDIT_EXISTING_DESK: 'reward/editexistingdesk',
};

const EmptyLayout = {
  layout_id: '',
  status: 1,
  sort_order: 0,
  name: '',
};

const EmptyDesk = {
  status: 1,
  sort_order: 0,
  position: {
    x: 0,
    y: 0,
  },
  name: '',
};

const DefaultStore = {
  loading: false,
  saving: false,
  deleting: false,
  error: {},
  layouts: [],
  desks: [],
  editingLayout: {},
  editingDesk: {},
};

export default function(store = DefaultStore, action) {
  switch (action.type) {
    case TYPES.GET_LAYOUT_LIST:
      return {
        ...store,
        loading: true,
        error: {},
      };

    case TYPES.GET_LAYOUT_LIST_SUCCESS:
      return {
        ...store,
        loading: false,
        layouts: action.layouts,
        error: {},
      };

    case TYPES.GET_LAYOUT_LIST_FAIL:
      return {
        ...store,
        loading: false,
        error: {
          type: TYPES.GET_LAYOUT_LIST_FAIL,
        },
      };

    case TYPES.CREATE_LAYOUT:
      return {
        ...store,
        editingLayout: EmptyLayout,
        error: {},
      };

    case TYPES.SAVE_LAYOUT:
      return {
        ...store,
        saving: true,
        error: {},
      };

    case TYPES.SAVE_LAYOUT_SUCCESS:
      const layouts = (action.is_new) ?
        [...store.layouts, action.layout] :
        store.layouts.map((item) => {
          if (item.server_layout_id === action.layout.layout_id) {
            return { ...item, ...action.layout };
          }

          return item;
        });

      return {
        ...store,
        layouts: layouts,
        editingLayout: {},
        saving: false,
        error: {},
      };

    case TYPES.SAVE_LAYOUT_FAIL:
      return {
        ...store,
        saving: false,
        error: {
          type: TYPES.SAVE_LAYOUT_FAIL,
        },
      };

    case TYPES.EDIT_EXISTING_LAYOUT:
      return {
        ...store,
        editingLayout: action.layout,
        error: {},
      };

    case TYPES.CANCEL_EDIT_LAYOUT:
      return {
        ...store,
        editingLayout: {},
        error: {},
      };

    case TYPES.DELETE_LAYOUT:
      return {
        ...store,
        deleting: true,
        error: {},
      };

    case TYPES.DELETE_LAYOUT_SUCCESS:
      return {
        ...store,
        deleting: false,
        layouts: store.layouts.filter(layout => layout.server_layout_id != action.layout_id),
        error: {},
      };

    case TYPES.DELETE_LAYOUT_FAIL:
      return {
        ...store,
        deleting: false,
        error: {
          type: TYPES.DELETE_LAYOUT_FAIL,
        },
      };

    case TYPES.GET_DESK_LIST:
      return {
        ...store,
        loading: true,
        error: {},
      };

    case TYPES.GET_DESK_LIST_SUCCESS:
      return {
        ...store,
        loading: false,
        desks: action.desks,
        error: {},
      };

    case TYPES.GET_DESK_LIST_FAIL:
      return {
        ...store,
        loading: false,
        error: {
          type: TYPES.GET_DESK_LIST_FAIL,
        },
      };


    case TYPES.ADD_DESK:
      const newDesk = {
        ...EmptyDesk,
      };

      return {
        ...store,
        editingDesk: newDesk,
        error: {},
      };

    case TYPES.CANCEL_EDIT_DESK:
      return {
        ...store,
        editingDesk: {},
        error: {},
      };

    case TYPES.SAVE_DESK:
      return {
        ...store,
        saving: true,
        error: {},
      };

    case TYPES.SAVE_DESK_SUCCESS:
      const desks = (action.is_new) ?
        [...store.desks, action.desk] :
        store.desks.map(item => {
          if (item.server_desk_id === action.desk.server_desk_id) {
            return { ...item, ...action.desk };
          }

          return item;
        });

      return {
        ...store,
        desks: desks,
        saving: false,
        error: {},
      };

    case TYPES.SAVE_DESK_FAIL:
      return {
        ...store,
        saving: false,
        error: {
          type: TYPES.SAVE_DESKS_FAIL,
        },
      };

    case TYPES.DELETE_DESK:
      return {
        ...store,
        deleting: true,
        error: {},
      };

    case TYPES.DELETE_DESK_SUCCESS:
      return {
        ...store,
        deleting: false,
        desks: store.desks.filter(desk => desk.server_desk_id != action.desk_id),
        error: {},
      };

    case TYPES.DELETE_DESK_FAIL:
      return {
        ...store,
        deleting: false,
        error: {
          type: TYPES.DELETE_DESK_FAIL,
        },
      };

    case TYPES.EDIT_EXISTING_DESK:
      return {
        ...store,
        editingDesk: action.desk,
        error: {},
      };

    case TYPES.SAVE_SEATS:
      return {
        ...store,
        saving: true,
        error: {},
      };

    case TYPES.SAVE_SEATS_SUCCESS:
      const newDesks = store.desks.map(item => {
        if (item.server_desk_id === action.desk_id) {
          return { ...item, seats: action.seats };
        }

        return item;
      });

      return {
        ...store,
        desks: newDesks,
        editingDesk: {},
        saving: false,
        error: {},
      };

    case TYPES.SAVE_SEATS_FAIL:
      return {
        ...store,
        saving: false,
        error: {
          type: TYPES.SAVE_SEATS_FAIL,
        },
      };

    default:
      return store;
  }
};